<?php

namespace addons\video\user\controller;

class VideoReport extends \web\user\controller\AddonUserBase{
    
    public function index(){
        return $this->fetch();
    }
    
    public function loadList(){
        $keyword = $this->_get('keyword');
        $startDate = $this->_get('startDate');
        $endDate = $this->_get("endDate");
        $filter = ' 1=1 ';
        if ($startDate != null && $endDate != null)
            $filter .= ' and (a.create_time >= \'' . $startDate . ' 00:00:00\' and a.create_time <= \'' . $endDate . ' 23:59:59\')';
        elseif ($startDate != null)
            $filter .= ' and (a.create_time >= \'' . $startDate . ' 00:00:00\')';
        elseif ($endDate != null)
            $filter .= ' and (a.create_time <= \'' . $endDate . ' 23:59:59\')';

        if ($keyword != null) {
            $filter .= ' and b.video_title like \'%' . $keyword . '%\' ';
        }

        $reportM = new \addons\video\model\VideoReport();
        $rows = $reportM->getVideoReportList($this->getPageIndex(), $this->getPageSize(), $filter);
        $total = $reportM->getVideoReportTotal($filter);
        return $this->toDataGrid($total, $rows);
    }

    //下架
    public function del(){
        if(IS_POST){
            $data['feedback'] = $this->_post('feedback');
            if($data['feedback'] == '') return $this->failData('请输入下架原因');
            $data['update_time'] = NOW_DATETIME;
            $data['is_dispose'] = 1;
            $data['id'] = $this->_post('id');
            $reportM = new \addons\video\model\VideoReport();
            $r_data = $reportM->getDetail($data['id']);
            $videoM = new \addons\video\model\Video();
            $re = $videoM->where(['id' => $r_data['video_id']])->update(['is_public' => 0]);
            if(!$re) return $this->failData('视频不存在或已下架');
            $re = $reportM->save($data);
            if(!$re) return $this->failData('删除失败');
            return $this->successData();
        }else{
            $this->assign('id', $this->_get('id'));
            return $this->fetch();
        }
    }

    //封号
    public function rejected(){
        if(IS_POST){
            $data['feedback'] = $this->_post('feedback');
            if($data['feedback'] == '') return $this->failData('请输入封号原因');
            $data['update_time'] = NOW_DATETIME;
            $data['is_dispose'] = 1;
            $data['id'] = $this->_post('id');
            $reportM = new \addons\video\model\VideoReport();
            $re = $reportM->save($data);
            if(!$re) return $this->failData('修改状态失败');
            $report_data = $reportM->getDetail($data['id']);
            $videoM = new \addons\video\model\Video();
            $video_user_id = $videoM->getDetail($report_data['video_id'], 'user_id')['user_id']??0;
            if($video_user_id < 1) return $this->failData('已处理');
            $accountM = new \addons\member\model\MemberAccountModel();
            $re = $accountM->where(['id' => $video_user_id])->update(['is_frozen' => 1, 'update_time' => NOW_DATETIME]);
            if(!$re) return $this->failData('封号失败');
            return $this->successData();
        }else{
            $this->assign('id', $this->_get('id'));
            return $this->fetch();
        }
    }

    public function play(){
        $this->assign('path', $this->_get('path'));
        return $this->fetch();
    }
}


